package com.wxl.demo.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;

public class RequestInterceptor implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		HttpSession session = request.getSession();
		Enumeration<String> attributeNames = session.getAttributeNames();
		while (attributeNames.hasMoreElements()){
			String name = attributeNames.nextElement();
			Object sessionId = session.getAttribute(name);
			System.out.println("name: "+name+" ,sessionId: "+sessionId);
		}
		System.out.println("===============>>>>>>当前uri：" + request.getRequestURI());
		Object username = session.getAttribute("username");
		if (username == null){
			System.out.println("未登录，请登录");
			response.sendRedirect(request.getContextPath()+"/login/toLogin");
			return false;
		}else {
			System.out.println("已登录，放行请求");
			return true;
		}
	}


}
